Skip to content

Improve stubs for flatten and flatten_fieldsets - #2572

Merged
sobolevn merged 2 commits into
typeddjango:masterfrom
ngnpope:improve-flatten-functions
Mar 25, 2025
Merged

sobolevn merged 2 commits into
typeddjango:masterfrom
ngnpope:improve-flatten-functions

Conversation

@ngnpope

@ngnpope ngnpope commented Mar 18, 2025 •

Copy link
Copy Markdown
Member

I have made things!

These functions are implemented in a generic way that doesn't generally care about the content. The existing return type of Callable | str makes these awkward to use.

I've changed the use of Sequence in the _FieldGroups alias to use _ListOrTuple. This is another case where ModelAdmin.fields and the "fields" item in fieldsets are presumed by Django to be a list or tuple, e.g. there are some system checks that ensure this.

See https://github.com/django/django/blob/afbb8c709d40e77b3f71c152d363c5ad95ceec2d/django/contrib/admin/utils.py#L102-L120

Related issues

N/A

@ngnpope
ngnpope force-pushed the improve-flatten-functions branch from 350a803 to 046aa7c Compare March 18, 2025 16:57
@ngnpope
ngnpope marked this pull request as ready for review March 18, 2025 17:11
@ngnpope

This comment was marked as outdated.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread django-stubs/contrib/admin/utils.pyi Resolved
@ngnpope
ngnpope force-pushed the improve-flatten-functions branch 4 times, most recently from 4c89ad4 to 2438ddb Compare March 25, 2025 16:41

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like pyright is not happy with something :(

@ngnpope
ngnpope force-pushed the improve-flatten-functions branch 2 times, most recently from d927064 to bca1c4d Compare March 25, 2025 16:59
@ngnpope

ngnpope commented Mar 25, 2025

Copy link
Copy Markdown
Member Author

Looks like pyright is not happy with something :(

Yeah... Looking into that. It's hard to get the different type checkers to agree on pretty much anything at all it seems 😞

I've fixed a bunch of the pyright issues, ignored a few around the model fields that can't be handled until we support making fields generic as per #2214, and a handful still to check.

The good news is that, by adding some testing, I ended up simplifying the implementation.

ngnpope added 2 commits March 25, 2025 17:12
This renames `_DisplayT` to `_ListDisplayT` and creates a new
`_DisplayT` alias with the contents of the `_ListOrTuple`.

This will be used in the next commit.
These functions are implemented in a generic way that doesn't
generally care about the content. The existing return type of
`Callable | str` makes these awkward to use.

I've changed the use of `Sequence` in the `_FieldGroups` alias to use
`_ListOrTuple`. This is another case where `ModelAdmin.fields` and the
`"fields"` item in fieldsets are presumed by Django to be a `list` or
`tuple`, e.g. there are some system checks that ensure this.

See https://github.com/django/django/blob/afbb8c709d40e77b3f71c152d363c5ad95ceec2d/django/contrib/admin/utils.py#L102-L120
@ngnpope
ngnpope force-pushed the improve-flatten-functions branch from bca1c4d to d1a968e Compare March 25, 2025 17:21
@ngnpope

ngnpope commented Mar 25, 2025

Copy link
Copy Markdown
Member Author

Success @sobolevn! Somehow it's turned out rather clean after my silly false starts! 😅

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks!

@sobolevn
sobolevn merged commit 4debb57 into typeddjango:master Mar 25, 2025
@ngnpope
ngnpope deleted the improve-flatten-functions branch March 25, 2025 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants